#include "noisegatewidget.h"
#include "dm/dmdef.h"

NoiseGateWidget::NoiseGateWidget(QWidget *parent)
    : QWidget{parent}
{

    thresholdLabel = new ALabel(tr(LABEL_THRESHOLD), this);
    thresholdSpinBox = new ASpinBox(this);
    ucs = new NoiseGateUCS(this);

    auto layout = new QGridLayout(this);

    layout->addWidget(thresholdLabel, 0, 0, Qt::AlignHCenter);
    layout->addWidget(thresholdSpinBox, 1, 0, Qt::AlignHCenter);
    layout->addWidget(ucs, 2, 0, 1, 4);

    thresholdSpinBox->setSuffix(tr(LABEL_DECIBEL_UINT));
    thresholdSpinBox->setRange(NOISE_GATE_THRESHOLD_MIN, NOISE_GATE_THRESHOLD_MAX);

    connect(thresholdSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &NoiseGateWidget::valueChanged);

    connect(ucs, &NoiseGateUCS::thresholdChanged, thresholdSpinBox, &QSpinBox::setValue);

    set(NoiseGate());
}

NoiseGate NoiseGateWidget::getData() const
{
    NoiseGate data;
    data.threshold = thresholdSpinBox->value();
    return data;
}

void NoiseGateWidget::set(const NoiseGate &data)
{
    blockSignals(true);
    thresholdSpinBox->setValue(data.threshold);
    blockSignals(false);
}

void NoiseGateWidget::valueChanged(const int &value)
{
    ucs->setThreshold(value);
    emit stateChanged(getData());
}

